package com.wasion.gydpe.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wasion.gydpe.model.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.wasion.gydpe.service.SystemService;

/**
 * 
 * @ClassName: ArchivesAction
 * @Description: 档案管理控制层Action
 * @author 高意
 * @date Sep 25, 2012 10:03:26 AM
 * 
 */
@ParentPackage("gydpe-default")
@Namespace("/system")
@Results( {
		@Result(name = "rightListSuccess", location = "/jsp/systemMgmt/rightMgmt.jsp"),
		@Result(name = "error", location = "/jsp/common/error/error.jsp") })
@ExceptionMappings( { @ExceptionMapping(exception = "java.lang.Exception", result = "error") })
@Controller
public class SystemAction extends BaseAction {

	private static final long serialVersionUID = -4389853345595838263L;

	private static final Logger LOGGER = LoggerFactory
			.getLogger(SystemAction.class);

	@javax.annotation.Resource
	private SystemService systemService;

	private String flag; // 标识
	private String msg = ""; // 消息
	private JSONObject jsonObject; // datagrid封装的json格式数据

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	/**
	 * 
	 * @Title: list
	 * @Description: 系统管理，根据flag标记的不同进行跳转
	 * @param
	 * @return
	 * @throws
	 */
	@Action(value = "list")
	public String list() {

		if (null != flag) {
			if ("right".equals(flag)) {
				return "rightListSuccess";
			} else if ("station".equals(flag)) {
				return "stationListSuccess";
			} else if ("line".equals(flag)) {
				return "lineListSuccess";
			} else if ("terminal".equals(flag)) {
				return "terminalListSuccess";
			} else if ("indicator".equals(flag)) {
				return "indicatorListSuccess";
			}
		}

		return "error";
	}

	/**
	 * 
	 * @Title: showUndistrituteds
	 * @Description: 显示未分配的资源（权限）
	 * @param
	 * @return
	 * @throws
	 */
	@Action(value = "showUndistrituteds")
	public void showUndistrituteds() throws Exception {

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("roleId", "2");
		List<List<Resource>> list = new ArrayList<List<Resource>>();

		try {
			list = systemService.queryUndistributeds(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("显示未分配的资源（权限）失败：showUndistrituteds >>>>>> " + e);
			msg = EXCEPTION_MSG;
		}

		JSONArray jsArray = JSONArray.fromObject(list);
		this.printToViewJustResult(jsArray.toString());
	}

	/**
	 * 
	 * @Title: showDistrituteds
	 * @Description: 显示已分配的资源（权限）
	 * @param
	 * @return
	 * @throws
	 */
	@Action(value = "showDistrituteds")
	public void showDistrituteds() throws Exception {

		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("roleId", "2");
		List<List<Resource>> list = new ArrayList<List<Resource>>();

		try {
			list = systemService.queryDistributeds(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("显示已分配的资源（权限）失败：showDistrituteds >>>>>> " + e);
			msg = EXCEPTION_MSG;
		}

		JSONArray jsArray = JSONArray.fromObject(list);
		this.printToViewJustResult(jsArray.toString());
	}
}
